<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">

<ui:composition template="/WEB-INF/templates/template.xhtml">
	<ui:define name="content">
		<script>
		// This is called with the results from from FB.getLoginStatus().
		function statusChangeCallback(response) {
			console.log('statusChangeCallback');
			console.log(response);
			// The response object is returned with a status field that lets the
			// app know the current login status of the person.
			// Full docs on the response object can be found in the documentation
			// for FB.getLoginStatus().
			if (response.status === 'connected') {
				// Logged into your app and Facebook.
				testAPI();
			} else if (response.status === 'not_authorized') {
				// The person is logged into Facebook, but not your app.
				console.log('Please log '
						+ 'into this app.');
			} else {
				// The person is not logged into Facebook, so we're not sure if
				// they are logged into this app or not.
				console.log('Please log '
						+ 'into Facebook.');
			}
		}

		// This function is called when someone finishes with the Login
		// Button.  See the onlogin handler attached to it in the sample
		// code below.
		function checkLoginState() {
			FB.getLoginStatus(function(response) {
				statusChangeCallback(response);
			});
		}

		window.fbAsyncInit = function() {
			FB.init({
				appId : '1382865341999264',
				cookie : true, // enable cookies to allow the server to access 
				// the session
				xfbml : true, // parse social plugins on this page
				version : 'v2.0' // use version 2.0
			});

			// Now that we've initialized the JavaScript SDK, we call 
			// FB.getLoginStatus().  This function gets the state of the
			// person visiting this page and can return one of three states to
			// the callback you provide.  They can be:
			//
			// 1. Logged into your app ('connected')
			// 2. Logged into Facebook, but not your app ('not_authorized')
			// 3. Not logged into Facebook and can't tell if they are logged into
			//    your app or not.
			//
			// These three cases are handled in the callback function.

			FB.getLoginStatus(function(response) {
				statusChangeCallback(response);
			});

		};

		// Load the SDK asynchronously
		(function(d, s, id) {
			var js, fjs = d.getElementsByTagName(s)[0];
			if (d.getElementById(id))
				return;
			js = d.createElement(s);
			js.id = id;
			js.src = "//connect.facebook.net/en_US/sdk.js";
			fjs.parentNode.insertBefore(js, fjs);
		}(document, 'script', 'facebook-jssdk'));

		// Here we run a very simple test of the Graph API after login is
		// successful.  See statusChangeCallback() for when this call is made.
		function testAPI() {
			console.log('Welcome!  Fetching your information.... ');
			FB
					.api(
							'/me',
							function(response) {
								console.log('Successful login for: '
										+ response.name);
								console.log('Successful login for: '
										+ response.username);
								console.log('Successful login for: '
										+ response.email);
								logarFB(response);
//								document.getElementById('status').innerHTML = 'Thanks for logging in, '
//										+ response.name + '!';
							});
		}

		function login() {
			FB.login(function(response) {
				checkLoginState();
			}, {scope: 'email'});
		}

		function criarInput(response, key) {
			var html = '';
			for ( var key1 in response) {
				if (key1 == key) {
					html += '<input type="hidden" id="' + key + 'FB" name="' + key + 'FB" value="';
				html += response[key] + '"/>';
				}
			}
			return html;
		}
		
		function logarFB(response) {

			var html = '';
			html += criarInput(response, "username");
			html += criarInput(response, "id");
			html += criarInput(response, "name");
			html += criarInput(response, "first_name");
			html += criarInput(response, "middle_name");
			html += criarInput(response, "last_name");
			html += criarInput(response, "email");

			document.getElementById('loginFBForm').innerHTML += html;
			document.getElementById('loginFBForm:loginFBBtn').click();

		}

		</script>


		<script type="text/javascript">
/*
			function verificaConexao() {
				//				var button = document.getElementById('fb-auth');
				//				button.innerHTML = 'Logout';
				//				button.className = 'btn';
				//				button.onclick = function() {
				//					FB.logout(function(response) {
				//					});
				//				};

				FB.api('/me', function(response) {
					logarFB(response);
				});

			}
*/
*
/*
			function updateButton(response) {
				var button = document.getElementById('fb-auth');

				if (response.status === 'connected') {
					verificaConexao();
				} else {
					button.innerHTML = '<img src="img/loginFB.png" alt=""/>';
					button.className = 'btn';
					button.onclick = function() {
						FB.login(function(response) {
							if (response.status === 'connected') {
								verificaConexao();
							}
						},{scope: 'email'});
					};
				}
			};

			$.getScript('//connect.facebook.net/en_UK/all.js', function() {
				FB.init({
					appId : '1382865341999264',
					channelUrl : '//localhost:8080/',
					status : true,
					cookie : true,
					xfbml : true
				});
				$('#fb-auth').removeAttr('disabled');
				FB.getLoginStatus(updateButton);
				FB.Event.subscribe('auth.statusChange', updateButton);
			});
*/
		</script>

		<div class="rbroundbox">
			<div class="rbtop">
				<div></div>
			</div>
			<div class="rbcontent">
				<div class="content">
					<h:form id="loginForm">
						<h2>Login</h2>
						<p>Digite o login e a senha para acessar</p>
						<div class="row">
							<div class="col-lg-8 col-sm-8 col-xs-8">
								<div class="form-group">
									<h:outputLabel value="Login"></h:outputLabel>
									<h:inputText styleClass="form-control"
										value="#{loginBean.login}" required="true"
										requiredMessage="Informe um login"></h:inputText>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-lg-4 col-sm-4 col-xs-4">
								<div class="form-group">
									<h:outputText value="Senha"></h:outputText>
									<h:inputSecret styleClass="form-control"
										value="#{loginBean.senha}" required="true"
										requiredMessage="Informa uma senha"></h:inputSecret>
								</div>
							</div>
						</div>
						<h:commandButton value="Login" class="btn" id="loginBtn"
							action="#{loginBean.logar}" />
					</h:form>


					<h:form>
						<br />
						<img src="img/loginFB.png" alt="" onclick="login();" />
						<div id="me"></div>
					</h:form>

				</div>
				<div id="FBDiv" style="visibility: hidden;">
					<h:form id="loginFBForm">
						<h:commandButton id="loginFBBtn" action="#{loginBean.logarFB}" />
					</h:form>
				</div>
				<h:form>
					<div id="panel-right">
						<div id="panel-right-inside">
							<h:commandButton styleClass="btn" action="novoUsuario"
								value="Criar nova senha" />
						</div>
					</div>
				</h:form>

			</div>
			<div class="rbbot">
				<div></div>
			</div>
		</div>
	</ui:define>
</ui:composition>
</html>